package com.dayaokeji.rhythmschoolstudent.utils;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ********************************************************************
 * *  Created by Zeno on 2018/7/30
 * ********************************************************************
 * *  version 1.0
 * ********************************************************************
 * *  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 * ********************************************************************
 * *课程表时间跨度
 * ********************************************************************
 */
public class CourseTableUtil {

    public static List<Map<String,String>> getWeeks() {
        // 输出星期几的日期
        List<Integer> daysOfOneWeek = new ArrayList();
        daysOfOneWeek.add(1);
        daysOfOneWeek.add(0);

        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;
        String startTime ;
        String endTime;
        if (month < 8) {
            startTime = calOneWeekDate(year+"-01-01");
            endTime = calSevenWeekDate(year+"-09-30");
        }else{
            startTime = calOneWeekDate(year+"-07-01");
            endTime = calSevenWeekDate(year+"-12-31");
        }
        return calWeeks(startTime, endTime, daysOfOneWeek);
    }

    private static String calOneWeekDate(String date) {
        Date formatDate = DateUtils.formatDate(date, DateUtils.FORMAT_DATE);
        Calendar cal = Calendar.getInstance();
        cal.setTime(formatDate);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if(1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        return DateUtils.format(cal.getTime(), DateUtils.FORMAT_DATE);
    }

    private static String calSevenWeekDate(String date) {
        Date formatDate = DateUtils.formatDate(date, DateUtils.FORMAT_DATE);
        Calendar cal = Calendar.getInstance();
        cal.setTime(formatDate);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if(1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, 6);
        return DateUtils.format(cal.getTime(), DateUtils.FORMAT_DATE);
    }

    private static List<Map<String,String>> calWeeks(String dateFrom, String dateEnd, List<Integer> weekDays) {
        List<String> daysNeedBookList = getDates(dateFrom, dateEnd, weekDays);
        System.out.println(daysNeedBookList.size()+"--");
        if (daysNeedBookList.size() % 2 != 0) {
            daysNeedBookList.remove(daysNeedBookList.size()-1);
        }
        List<Map<String,String>> maps = new ArrayList<>();
        for (int i = 0; i < daysNeedBookList.size() / 2; i++) {
            Map<String,String> map = new HashMap<>();
            map.put("start",daysNeedBookList.get((i * 2)));
            map.put("end",daysNeedBookList.get((i *2+1)));
            maps.add(map);
        }

        // test
        /*for (Map<String, String> map : maps) {
            System.out.println("start == "+map.get("start")+"====== end -- "+map.get("end"));
        }*/
        return maps;
    }


    /**
     * 输入一个日期的时间段，以及相应的星期数，获得这些星期的日期
     */
    private static Map<Integer, Integer> weekNumberMap = new HashMap();

    static {
        weekNumberMap.put(0, 1);
        weekNumberMap.put(1, 2);
        weekNumberMap.put(2, 3);
        weekNumberMap.put(3, 4);
        weekNumberMap.put(4, 5);
        weekNumberMap.put(5, 6);
        weekNumberMap.put(6, 7);
    }


    private static List<String> getDates(String dateFrom, String dateEnd, List<Integer> weekDays) {
        long time;
        long perDayMilSec = 24L * 60 * 60 * 1000;
        List<String> dateList = new ArrayList<>();
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 需要查询的星期系数
        String strWeekNumber = weekForNum(weekDays);
        try {
            dateFrom = sdf.format(sdf.parse(dateFrom).getTime() - perDayMilSec);
            while (true) {
                time = sdf.parse(dateFrom).getTime();
                time = time + perDayMilSec;
                Date date = new Date(time);
                dateFrom = sdf.format(date);
                if (dateFrom.compareTo(dateEnd) <= 0) {
                    // 查询的某一时间的星期系数
                    Integer weekDay = dayForWeek(date);
                    // 判断当期日期的星期系数是否是需要查询的
                    if (strWeekNumber.contains(weekDay.toString())) {
                        dateList.add(dateFrom);

                    }
                } else {
                    break;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateList;
    }


    // 等到当期时间的周系数。星期日：1，星期一：2，星期二：3，星期三：4，星期四：5，星期五：6，星期六：7
    private static Integer dayForWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }


    /**
     * 得到对应星期的系数 0：1，星期
     */
    private static String weekForNum(List<Integer> weekDays) {
        // 返回结果为组合的星期系数
        String weekNumber = "";
        for (Integer weekDay : weekDays) {
            weekNumber = weekNumber + "" + getWeekNum(weekDay).toString();
        }
        return weekNumber;


    }


    // 将星期转换为对应的系数 0,星期日; 1,星期一; 2....
    private static Integer getWeekNum(int strWeek) {
        return weekNumberMap.get(strWeek);
    }

}
